<!DOCTYPE html>
<html lang="zh-cmn-Hans" prefix="og: http://ogp.me/ns#" class="han-init">
<head>
  <meta charset="utf-8">
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <link rel="icon" href="http://upeng.github.io/favicon.ico">
  <title>upeng</title>
  <link rel="stylesheet" href="/css/style.css">
  <link rel="stylesheet" href="/lib/fancybox/jquery.fancybox-1.3.4.css">
  <!--在这里倒入jquery 方便处理部分页面的jquery-->
  <script src="https://cdn.staticfile.org/jquery/1.7/jquery.min.js" type="text/javascript" ></script>
</head>

<body>
	<header class="site-header navfixed-false">
  <div class="container">
      <h1><a href="/" title="upeng"><span class="octicon octicon-mark-github"></span> upeng</a></h1>
      <nav class="site-header-nav" role="navigation">
        
              
              <a href="/"  class=" site-header-nav-item hvr-underline-from-center" title="Home">Home</a>
        
              
              <a href="/categories/"  class=" site-header-nav-item hvr-underline-from-center" title="Categories">Categories</a>
        
              
              <a href="/bookmark/"  class=" site-header-nav-item hvr-underline-from-center" title="Bookmark">Bookmark</a>
        
              
              <a href="http://shareup.sinaapp.com"  class=" site-header-nav-item hvr-underline-from-center" title="Share">Share</a>
        
              
              <a href="/about/"  class=" site-header-nav-item hvr-underline-from-center" title="About">About</a>
        
      </nav>
  </div>
</header>

	
<section class="collection-head geopattern" data-pattern-id="laravel初体验" >
    <div class="container">
        <div class="collection-title">
            <h1 class="collection-header">
                laravel初体验
            </h1>
            
                <div class="collection-info">
                    <span class="meta-info">
                        <span class="octicon octicon-calendar"></span>
                        <time datetime="2016-04-25T12:10:00.000Z" itemprop="datePublished">2016-04-25</time>
                    </span>
                    
                        <span class="meta-info">
                            <span class="octicon octicon-file-directory"></span>
                            <a href='/categories/PHP/' title=''>PHP</a>
                        </span>
                    
                </div>
            
        </div>
    </div>
</section>
	<section class="container">
    <div class="columns">
        <!-- -->
        <div class="column three-fourths">
            <article class="article-content markdown-body">
                <h3 id="访问流程"><a href="#访问流程" class="headerlink" title="访问流程"></a>访问流程</h3><blockquote>
<p>路由文件routes.php</p>
</blockquote>
<pre><code>/*默认是以匿名函数的方式进行路由,/是当前域名下加载视图文件welcome.blade.php */

Route::get(&#39;/&#39;, function () {
    return view(&#39;welcome&#39;);
});
Route::get(&#39;/about&#39;, function () {
    return view(&#39;about&#39;);
});
Route::get(&#39;/blog&#39;, function () {
    return view(&#39;blog.content&#39;);  //其中的“.”可以用“/”代替
});

//如果使用非匿名函数的方式

Route::get(&#39;/&#39;, &#39;UserController@index&#39;);
Route::get(&#39;/about&#39;, &#39;UserController@about&#39;);
Route::get(&#39;/blog&#39;, &#39;UserController@blog&#39;);
</code></pre><h3 id="创建控制器"><a href="#创建控制器" class="headerlink" title="创建控制器"></a>创建控制器</h3><blockquote>
<ol>
<li>可以手动创建Controller，也可以使用命令行创建控制器<code>php artisan</code>工具</li>
<li>参数 –plain使用则不会生成一些默认的方法如index，create等</li>
</ol>
</blockquote>
<pre><code>php artisan make:controller UserController
php artisan make:controller TestController --plain
</code></pre><h3 id="视图的加载"><a href="#视图的加载" class="headerlink" title="视图的加载"></a>视图的加载</h3><blockquote>
<p>加载视图的方法</p>
</blockquote>
<pre><code>return view(&#39;welcome&#39;);
return view(&#39;blog/title&#39;);
return view(&#39;blog.content&#39;);
</code></pre><blockquote>
<p>怎么向blade视图传递变量</p>
</blockquote>
<pre><code>$name = &#39;&lt;p style=&quot;color:red&quot;&gt;upfriend&lt;/p&gt;&#39;;
$time = date(&#39;Y-m-d&#39;, time());
$arr = array(&#39;first&#39; =&gt; &#39;jerry&#39;, &#39;last&#39; =&gt; &#39;bool&#39;);

//传递变量-通用方式传递给$data数组
$data[&#39;name&#39;] = $name;
$data[&#39;time&#39;] = $time; 
return view(&#39;blog.content&#39;, $data);

//Laravel链式操作方式 
return view(&#39;blog.content&#39;)-&gt;(&#39;name&#39; =&gt; $name);  //传递一个变量
return view(&#39;blog.content&#39;)-&gt;with(array(&#39;name&#39; =&gt; $name, &#39;time&#39; =&gt; $time));  //传递数组变量

//compact方法
return view(&#39;blog.content&#39;, compact(&#39;name&#39;, &#39;time&#39;, &#39;arr&#39;));  //去掉$的变量名
</code></pre><blockquote>
<p>如何正确输出变量的值</p>
</blockquote>
<pre><code>//原生PHP输出数据方式，默认会对变量中的字符会进行转义（xss?）
&lt;h1&gt;&lt;?=name?&gt;&lt;/h1&gt;   

//Laravel的blade模板引擎渲染方式，两个大括号{{}}表示转义后输出
&lt;h1&gt;{{name}}&lt;/h1&gt;  //浏览器输出&lt;p style=&quot;color:red&quot;&gt;upfriend&lt;/p&gt;

&lt;h1&gt;{!!name!!}&lt;/h1&gt;  //会见变量作为html,输出红色字体的upfriend
</code></pre>
            </article>
            
                <div class="share">
                    <!--开启分享-->
<div class="share-component" data-disabled="google,twitter,facebook" data-description=""></div>

<script src="/js/share.min.js"></script>

                </div>    
            
            
                
<div class="comments">
	<div class="ds-thread" data-thread-key="laravel-primary" data-title="laravel初体验" data-url="http://upeng.github.io/2016/04/25/laravel-primary/"></div>
	<script type="text/javascript">
	var duoshuoQuery = {short_name:"upeng"};
		(function() {
			var ds = document.createElement('script');
			ds.type = 'text/javascript';ds.async = true;
			ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
			ds.charset = 'UTF-8';
			(document.getElementsByTagName('head')[0]
			 || document.getElementsByTagName('body')[0]).appendChild(ds);
		})();
	</script>
</div>

            
        </div>
        <div class="column one-fourth">
            
                
                


<h3>Post Directory</h3>

<div id="post-directory-module">
	<section class="post-directory">
		<p><strong class="toc-title">文章目录</strong></p>
		<ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#访问流程"><span class="toc-text">访问流程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#创建控制器"><span class="toc-text">创建控制器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#视图的加载"><span class="toc-text">视图的加载</span></a></li></ol>
	</section>
</div>
            
        </div>
    </div>
</section>

<footer class="container">
    <div class="site-footer" role="contentinfo">
        <div class="copyright left mobile-block">
                © 2016
                <span title="yupeng">yupeng</span>
                <a href="javascript:window.scrollTo(0,0)" class="right mobile-visible">TOP</a>
        </div>

        <ul class="site-footer-links right mobile-hidden">
            <li>
                <a href="javascript:window.scrollTo(0,0)" >TOP</a>
            </li>
        </ul>

        <a href="https://github.com/upeng" target="_blank" aria-label="view source code">
            <span class="mega-octicon octicon-mark-github" title="GitHub"></span>
        </a>

        <ul class="site-footer-links mobile-hidden">
            
                  
                  <li>
                    <a href="/"  title="Home">Home</a>
                  </li>
            
                  
                  <li>
                    <a href="/categories/"  title="Categories">Categories</a>
                  </li>
            
                  
                  <li>
                    <a href="/bookmark/"  title="Bookmark">Bookmark</a>
                  </li>
            
                  
                  <li>
                    <a href="http://shareup.sinaapp.com"  title="Share">Share</a>
                  </li>
            
                  
                  <li>
                    <a href="/about/"  title="About">About</a>
                  </li>
            
            <li>
                <a href="/atom.xml">
                    <span class="octicon octicon-rss" style="color:orange;"></span>
                </a>
            </li>
        </ul>
    </div>
</footer>


		<script src="/js/geopattern.js"></script>

		
			<script src="/js/toc.js"></script>
		

		<script src="/js/highlight.pack.js"></script>
		<script src="/lib/fancybox/jquery.fancybox-1.3.4.pack.js"></script>

		<script src="/js/index.js"></script>

		 <script src="/js/popular_repo.js"></script> 

	</body>
</html>